/***************************************************************************
 *   Copyright (C) 2007 by Leonid Krashenko   *
 *   krashenko@mail.ru   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "Environment.h"
#include "iEnvExporter.h"
#include "neiderra/utils/Logger.h"

using namespace neiderra::core;
using neiderra::utils::Logger;

Environment::Environment(Core* core)
	:Base(core)
{
}

Environment::~Environment()
{
}

void Environment::registerMethod(const std::string& name, iEnvExporter* exporter)
{
	_entries[name]=exporter;
}

void Environment::executeMethod(const string& name)
{
	list<string> args;
	executeMethod(name, args);
}

void Environment::executeMethod(const std::string& name, const list<string>& args)
{
	// find exporter for this method
	map<string, iEnvExporter*>::iterator i = _entries.find(name);
	
	if(i==_entries.end())
	{
		getCore()->getLogger()->log("can't find " + name);
		return;	
	}
	
	// execute method
	iEnvExporter* exporter = (*i).second;
	if(exporter)
		exporter->executeMethod(name, args);
}

void Environment::registerVariable(const std::string& name, iEnvExporter* exporter)
{
	// think
}

list<string> Environment::getCmdList()
{
	list<string> cmdList;
	map<string, iEnvExporter*>::iterator i = _entries.begin();
	for(;i!=_entries.end();i++)
	{
		cmdList.push_back(i->first);
	}
	return cmdList;
}
